﻿/*
 *	Copyright (c) 2009 Queensland University of Technology. All rights reserved.
 *	The QUT Bioinformatics Collection is open source software released under the 
 *	Microsoft Public License (Ms-PL): http://www.microsoft.com/opensource/licenses.mspx.
 */
using System;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using QUT.Bio.SilverMap.ObjectModel;
using QUT.Bio.Util;
using System.Linq;

namespace QUT.Bio.SilverMap {
	public class SequenceSummary : IComparable<SequenceSummary>, IEquatable<SequenceSummary>, ISelectable {

		private ViewModel model;
		private ILinearDomain sequence;
		private ObjectWrapper sequenceWrapper;
		private bool selected;

		public SequenceSummary (
			ViewModel model,
			ILinearDomain sequence
		) {
			this.model = model;
			this.sequence = sequence;
			sequenceWrapper = new ObjectWrapper( sequence );
		}

		public ILinearDomain Sequence {
			get {
				return sequence;
			}
		}

		public bool Selected {
			get {
				return selected;
			}
			set {
				if ( value != selected ) {
					selected = value;

					if ( PropertyChanged != null ) {
						PropertyChanged( this, new PropertyChangedEventArgs( "Selected" ) );
					}
				}
			}
		}

		public double DistanceFromReferenceSequence {
			get {
				return model.DistanceFromReferenceSequence( sequence );
			}
		}

		public string Product {
			get {
				return sequenceWrapper.HasProperty( "Product" )
					? sequenceWrapper["Product"].ToString()
					: "n/a";
			}
		}

		#region Property: GIs
		/// <summary>
		/// Gets a comma-separated list of GI numbers associated with this sequence, 
		/// if there are any.
		/// <para>
		/// This caters for the situation where multiple organisms contain the same gene, as in the 
		/// NCBI non-redundant blast db.
		/// </para>
		/// </summary>

		public string GIs {
			get {
				var xrefs = from xref in sequence.Xref
							where xref.Database.Equals( "GI", StringComparison.InvariantCultureIgnoreCase )
							select xref.Id;

				string result = xrefs.Join( "," );

				if ( result.Equals( String.Empty ) ) {
					result = "n/a";
				}

				return result;
			}
		}
		#endregion

		#region Event: PropertyChanged
		/// <summary>
		/// Subscribe to the PropertyChanged event to discover when the 
		/// sequence has been selected or deselected.
		/// </summary>

		public event PropertyChangedEventHandler PropertyChanged;
		#endregion

		public int CompareTo ( SequenceSummary other ) {
			return this.sequence.CompareTo( other.sequence );
		}

		public bool Equals ( SequenceSummary other ) {
			return this.sequence.Equals( other.sequence );
		}
		
		public string Name {
			get {
				return sequence.Name;
			}
		}
		
		public string Label {
			get {
				return sequence.Label;
			}
		}

		public string SelectableText {
			get {
				int nameLen = Math.Min( 60, Name.Length ); 
				return Label + ": " + Name.Substring( 0, nameLen );
			}
		}

		public int CompareTo ( object obj ) {
			return CompareTo( (SequenceSummary) obj );
		}
	}
}
